package com.cxhc.demo1005;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Random;

public class GoodAdapter extends RecyclerView.Adapter {
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_TEXT) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_text, parent, false);
            return new MultiViewHolder(view);
        } else if (viewType == TYPE_STAGER) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_text2, parent, false);
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height = 50+ new Random().nextInt(30);
            layoutParams.width =getScreenWidth(parent.getContext())/2 -10;
            view.setLayoutParams(layoutParams);
            return new MultiViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_more, parent, false);
            return new MultiViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return 30;
    }

    @Override
    public int getItemViewType(int position) {
//        if (position<3){
//            return TYPE_BANNER;
//        }
        if (position == 0) {
            return TYPE_BANNER;
        } else if (position <= 4 && position > 0) {
            return TYPE_GRID1;
        } else if (position <= 10 && position > 4) {
            return TYPE_GRID2;
        } else if (position % 7 == 0) {
            return TYPE_TEXT;
        } else {
            return TYPE_STAGER;
        }
//        return super.getItemViewType(position);
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull final RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type) {
                        case TYPE_BANNER:
                        case TYPE_TEXT:
                            return 6;
                        case TYPE_GRID1:
//                            if (position>=8&&position<9){
//                                gridLayoutManager.setSpanCount(4);
//                            }
                            return 3;
                        case TYPE_GRID2:
                            return 2;
                        case TYPE_STAGER:
                            return 2;
                        default:
                            return 3;
                    }
//                    return 0;
                }
            });

//            gridLayoutManager.setSpanCount(4);
        }
    }


    public final int TYPE_BANNER = 0;
    public final int TYPE_GRID1 = 1;
    public final int TYPE_GRID2 = 2;
    public final int TYPE_STAGER = 3;

    public final int TYPE_TEXT = 4;
//    enum{
//        int TYPE_BANNER,
//    }


    static class MultiViewHolder extends RecyclerView.ViewHolder {

        public MultiViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public int getScreenWidth(Context context){
        WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return window.getDefaultDisplay().getWidth();
    }
}
